package com.example.aliyun.plus;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

class CallAbleThread implements Callable<String>{

    @Override
    public String call() throws Exception {
        return "this is callAble Thread";
    }
}

/**
 * Callable 和Runnable 区别
 * 1. Runnable 是java 1.0 出的， Callable 是java 1.5 之后出的
 * 2. 都是接口，实现方式不同：Callable 是重写 call 方法， Runnable 是重写 run 方法
 * 3. Callable 可以获取返回值， Runnable不能获取
 */
public class CallableDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CallAbleThread callAbleThread = new CallAbleThread(); // 实例
        FutureTask<String> stringFutureTask = new FutureTask<>(callAbleThread); // FutureTask构造
        new Thread(stringFutureTask).start(); // start 方法启动
        String name = stringFutureTask.get(); // 获取返回值
        System.out.println(name);
    }
}
